package com.aiiju.core.cont;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.aiiju.core.util.FilePathUtils;
import com.aiiju.core.util.txtParse;
import com.tongdainfo.commom.util.FileToString;

@Controller
public class TestIndex {
	
	@RequestMapping(value="/test.do")
	public String TestControl() {
		String path = FilePathUtils.getPath();
		System.err.println(path);
		
		String resource = path + "resources";
		System.err.println(resource);
		return "index";
	}
	@RequestMapping(value="/upload.do")
	public String upload(@RequestParam(required = false)MultipartFile uploadFile ,Model model) throws Exception {
		
		String originalFilename = uploadFile.getOriginalFilename();
		
		boolean blank = StringUtils.isNotBlank(originalFilename);
		if(blank) {
			InputStream inputStream = uploadFile.getInputStream();
			FileToString fileToString = new FileToString();
			String fileContent = fileToString.getFileContent(originalFilename, inputStream);
			
			boolean hasContent = StringUtils.isNotBlank(fileContent);
			if(hasContent) {
				model.addAttribute("fileContext",fileContent);
				
				txtParse txtParse = new txtParse();
				String parseTxt = txtParse.parseTxt(fileContent);
				model.addAttribute("jsonContext",parseTxt);
			}
		}
		
		return "index";
	}

}
